﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using WatiN.Core;
using WebAutomation.Browsers;

namespace WebAutomation
{
    public class EndTestCommand : CommandBase
    {
        public EndTestCommand(WebBrowserObject browserObject)
        {
            this.Initialize(browserObject);

        }
        public override bool ExecuteCommand(WebBrowserObject browserObject, string[] parameter, string url, string testRepo, string testResultLocation, string dbConnString, ref System.Data.DataSet dataFromDB, ref TestLineResult testLineResult, ref bool stopTest, WebAutoUtils.ITestDataExceptionRules testDataExceptionRules)
        {
            string command = parameter[0];
            testLineResult.LineCommand = command;
            testLineResult.LineParameter = parameter[1];
            if (this.BrowserObject == null)
            {
                this.BrowserObject = browserObject;
            }
            bool openBlankBrowser = false;
            if (!string.IsNullOrEmpty(parameter[1]))
            {
                try
                {
                    openBlankBrowser = parameter[1].ToLower() == "true";
                }catch
                {
                    
                }
            }
            try
            {

                switch (command.ToLower())
                {
                    case "end":
                        testLineResult.LinePassed = EndTest();
                        break;


                }
                
            }
            catch (Exception ex)
            {
                testLineResult.IsFrameworkError = true;
                testLineResult.LineMessage = "Error." + ex.Message + "." + ex.StackTrace;
                testLineResult.LinePassed = false;
            }
            return testLineResult.LinePassed;
        }

        public virtual bool EndTest()
        {
            TestFileReader.RemoveExcelData();
            this.BrowserObject.Dispose();
            return TestCommands.KillBrowserProcess(this.BrowserObject.ProcessID, true);


        }
    }
}
